package longrise.phone.com.bjjt_jyb.myaccident;

import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.android.FormParameter;
import com.longrise.android.LFView;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_jyb.Adapter.PageAdapter;
import longrise.phone.com.bjjt_jyb.R;


/**
 * 我的事故主界面
 */
public class AccidentMainView extends LFView implements ViewPager.OnPageChangeListener{

    private Context mContext;
    private AccidentListView dealLView;//待处理列表

    private AccidentListView finishLView;//已处理列表
    private View view;
    private ViewPager page_vp;
    private TabLayout tb_history;
    private boolean isOver=true;

    public AccidentMainView(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        view = View.inflate(mContext, R.layout.view_accidentmain, null);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText("历史案件");
        LinearLayout hn_btn_back = (LinearLayout) view.findViewById(R.id.hn_btn_back);
        hn_btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeForm();
                OnDestroy();
            }
        });
        page_vp = (ViewPager) view.findViewById(R.id.page_vp);
        tb_history = (TabLayout) view.findViewById(R.id.tb_history);
        initTabLayout();
        dealLView.refresh();
    }

    private void initTabLayout() {
        List<LFView> lists = new ArrayList<>();
        String[] tabTitle = new String[]{"处理中", "已完成"};
        if (null == dealLView) {
            dealLView = new AccidentListView(mContext, 0);
            dealLView.init();
        }
        lists.add(dealLView);
        if (null == finishLView) {
            finishLView = new AccidentListView(mContext, 1);
            finishLView.init();
        }
        lists.add(finishLView);
        PageAdapter adapter = new PageAdapter();
        adapter.setLists(lists);
        adapter.setTitles(tabTitle);
        page_vp.setAdapter(adapter);
        page_vp.setCurrentItem(0);
        page_vp.addOnPageChangeListener(this);
        tb_history.setTabMode(TabLayout.MODE_FIXED);
        tb_history.setupWithViewPager(page_vp);
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter form = new FormParameter();
        form.setShowtitle(false);
        return form;
    }

    @Override
    public void refresh() {

    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        if (null != dealLView) {
            dealLView.OnDestroy();
        }
        dealLView = null;
        if (null != finishLView) {
            finishLView.OnDestroy();
        }
        finishLView = null;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if(position==1&&isOver){
            finishLView.refresh();
            isOver=false;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
